package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import pojo.MerchantApplication;
import util.DBUtils;
import dao.MerchantApplicationDao;

public class MerchantApplicationDaoImpl extends BaseDaoImpl<MerchantApplication> implements MerchantApplicationDao{

	@Override
	public int add(MerchantApplication merchantApplication) {
		String sql = "insert into merchant_applications values(?,?,?,?,?)";
		PreparedStatement pst = null;
		int m = 0;
		Connection conn = DBUtils.createConnection();
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, UUID.randomUUID().toString());
			pst.setString(2, merchantApplication.getName());
			pst.setString(3, merchantApplication.getIdCard());
			pst.setString(4, merchantApplication.getAvatar());
			pst.setString(5, merchantApplication.getStatus());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, null);
		}
		return m;
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(MerchantApplication t) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public MerchantApplication load(String id) {
		String sql = "select * from merchant_applications where id=?";
		Connection conn = DBUtils.createConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		MerchantApplication merchantApplicationApplication = null;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, id);
			rs = pst.executeQuery();
			if(rs.next()) {
				merchantApplicationApplication = new MerchantApplication();
				merchantApplicationApplication.setId(rs.getString("id"));
				merchantApplicationApplication.setName(rs.getString("name"));
				merchantApplicationApplication.setIdCard(rs.getString("id_card"));
				merchantApplicationApplication.setAvatar(rs.getString("avatar"));
				merchantApplicationApplication.setStatus(rs.getString("status"));
			}
		} catch (Exception e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, rs);
		}
		return merchantApplicationApplication;
	}

	@Override
	public List<MerchantApplication> findAll() {
		String sql = "select * from merchant_applications";
		Connection conn = DBUtils.createConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<MerchantApplication> merchantApplications = new ArrayList<>(); 
		try {
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()) {
				MerchantApplication merchantApplication = new MerchantApplication();
				merchantApplication.setId(rs.getString("id"));
				merchantApplication.setName(rs.getString("name"));
				merchantApplication.setIdCard(rs.getString("id_card"));
				merchantApplication.setAvatar(rs.getString("avatar"));
				merchantApplication.setStatus(rs.getString("status"));
				merchantApplications.add(merchantApplication);
			}
		} catch (Exception e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, rs);
		}
		return merchantApplications;	
	}

	@Override
	public int updateStatus(String name, String status) {
		String sql = "update merchant_applications set status=?, where name=?";
		PreparedStatement pst = null;
		int m = 0;
		Connection conn = DBUtils.createConnection();
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, status);
			pst.setString(2, name);
			m = pst.executeUpdate();
		} catch (SQLException e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, null);
		}
		return m;
	}

}
